package org.tictactoe;

import org.tictactoe.corba.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import java.util.Scanner;
// org.tictactoe.TictactoeClient
public class TictactoeClient {
	private static Scanner scanner = new Scanner(System.in);
	static TictactoeService service;
	
	public static void main(String[] args) {

	
	    try {
	      // Initialize the ORB.
	    	ORB orb = ORB.init(args, null);
	
	        // get the root naming context
	        org.omg.CORBA.Object objRef = 
		    orb.resolve_initial_references("NameService");
	        // Use NamingContextExt instead of NamingContext. This is 
	        // part of the Interoperable naming Service.  
	        NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
	        service = TictactoeServiceHelper.narrow(ncRef.resolve_str("TictactoeService"));
	        System.out.println("Connection - OK!");
	    }
		catch (Exception e) {
	    	e.printStackTrace();
	    }
		try {
	        int[][] board = new int[3][3];
	        for (int i=0; i<3; i++)
				for (int j=0; j<3; j++)
			  		board[i][j] = 0; 
	        int team  = service.startGame();
		  
		    if (team == 1) System.out.println("You play crosses.");
		    if (team == 2) System.out.println("You play noughts.");
			if (team == 3) {
				System.out.println("Server is busy now :(");
				return;
			}
		    System.out.println("Waiting for opponent");
		    while (!service.isStarted()) {
			  try {
					Thread.sleep(500);
					continue;
				} catch (InterruptedException e) {
					break;
				}
		    }
		    System.out.println("Game on!");
	      
		    while (service.isEnded() == 0) {
			    Step step = service.getLastStep();
			    if (step.team != team) {
				    if ((step.col != -1) && (step.row != -1)) {
				    	board[step.row][step.col] = step.team;
				    	System.out.println("Opponent step - (" + (step.col+1) + ", " + (step.row+1) + ")");
				    }
				  
				    drawBoard(board);
				    Step myStep; 
				    boolean correct;
				    do {
				    	System.out.println("Your step: ");
					    int row = scanner.nextInt();
					    int col = scanner.nextInt();
						myStep = new Step(row - 1, col - 1, team);
					    correct = service.makeStep(myStep);
					    if (!correct) System.out.println("Wrong enter! Repeat please."); 
				   } while (!correct);
				   board[myStep.row][myStep.col] = team;
				   drawBoard(board);
			    }
			    Thread.sleep(100);
		    }
		    if (service.isEnded() == team) System.out.println("You win!");
		    else if (service.isEnded() == 3) System.out.println("Tie!");
		    else if (service.isEnded() == 4) System.out.println("Your opponent left game :(");
			else System.out.println("You lose :(");
		    service.endGame(0);
		  
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
			service.endGame(0);
	    }
	    
	    
	}
	
	private static void drawBoard(int[][] board) {
		for (int i=0; i<3; i++) {
			  for (int j=0; j<3; j++) {
				  if (board[i][j] == 0) System.out.print(" _ ");
				  if (board[i][j] == 1) System.out.print(" x ");
				  if (board[i][j] == 2) System.out.print(" o ");
			  }
			  System.out.println();
		  }
	}
	
}
